<?php


namespace Module\Member\Admin\Controller;


use App\Models\Blog;
use Illuminate\Routing\Controller;
use ModStart\Admin\Concern\HasAdminQuickCRUD;
use ModStart\Admin\Layout\AdminCRUDBuilder;
//use ModStart\Core\Type\TypeUtil;
use ModStart\Field\AbstractField;
use ModStart\Grid\GridFilter;
use ModStart\Support\Concern\HasFields;

//use Module\Member\Auth\MemberUser;
//use Module\Member\Util\MemberCmsUtil;
//use Module\Vendor\Type\OrderStatus;
use ModStart\Form\Form;
use ModStart\Core\Input\Response;
use Illuminate\Support\Facades\DB;

class MemberBindWalletController extends Controller
{
    use HasAdminQuickCRUD;

    protected function crud(AdminCRUDBuilder $builder)
    {
        $builder
            ->init('bindwallet')
            ->field(function ($builder) {
                /** @var HasFields $builder */
                $builder->display('id', '业务订单ID');
//                $builder->text('blog_id', '账户名称')->hookRendering(function (AbstractField $field, $item, $index) {
//                    return (new Blog())->where(['id' => $item->blog_id])->value("title");
//                });
//                $builder->text('user_id', '用户名称')->hookRendering(function (AbstractField $field, $item, $index) {
//                    return (new \App\Models\MemberUser())->where(['id' => $item->user_id])->value("nickname");
//                });
                $builder->select('blog_id', '账户名称')->optionModel('blog', 'id', 'title')->readonly(true);
                $builder->select('user_id', '用户名称')->optionModel('member_user', 'id', 'nickname')->readonly(true);
                $builder->display('phone', '手机号')->hookRendering(function (AbstractField $field, $item, $index) {
                    return (new \App\Models\MemberUser())->where(['id' => $item->user_id])->value("phone");
                });
                $builder->text('rmb_price', '金额/数量')->readonly(true);
                $builder->text('recharge_moeny', '本次充值金额')->defaultValue(0);
                $builder->datetime('created_at', '创建时间')->readonly(true);
//                $builder->type('status', '状态')->type(OrderStatus::class);
            })->defaultOrder(['user_id', 'desc'])
            ->gridFilter(function (GridFilter $filter) {
//                $filter->eq('id', '业务订单ID');
//                $filter->eq('phone', '手机号');
                $filter->eq('user_id', '手机号')->selectModel('member_user', 'id', 'phone');
                $filter->eq('user_id', '用户')->selectModel('member_user', 'id', 'nickname');
                $filter->eq('blog_id', '账户名称')->selectModel('blog', 'id', 'title');
//                $filter->eq('status', '状态')->select([
//                    OrderStatus::WAIT_PAY => TypeUtil::name(OrderStatus::class, OrderStatus::WAIT_PAY),
//                    OrderStatus::COMPLETED => TypeUtil::name(OrderStatus::class, OrderStatus::COMPLETED),
//                ]);
//            })->canEdit(false)->canDelete(false)
            })->canDelete(false)->canAdd(false)
//            ->disableCUD(true)
            ->title('用户-钱包充值订单');
    }

    public function form()
    {
        $form = Form::make('bindwallet');
        $form->select('blog_id', '账户名称')->optionModel('blog', 'id', 'title')->readonly(true);
        $form->select('user_id', '用户名称')->optionModel('member_user', 'id', 'nickname')->readonly(true);
        $form->text('rmb_price', '金额/数量')->readonly(true);
        $form->text('recharge_moeny', '充值金额')->value(0);
        $form->hookSaving(function (Form $form) {
            if ($form->isModeEdit()) {
                // 修改
                $data = $form->dataEditing();
                if($data['recharge_moeny'] <= 0){
                    return Response::generate(-1, '请输入充值金额!');
                }
                if ($data['recharge_moeny'] > 0 && $data['blog_id'] != 1) {
                    return Response::generate(-1, '充值只能对人民币充值,其他货币不允许充值!');
                }
                if ($data['recharge_moeny'] > 0 && $data['blog_id'] == 1) {
                    // 替换入库数据
                    $form->dataEditing($data);
                }
            }
        });
        $form->hookSaved(function (Form $form) {
            $item = $form->item();
            if ($form->isModeEdit()) {
                // 修改
                if ($item['recharge_moeny'] > 0 && $item['blog_id'] == 1) {
                    DB::table("bindwallet")->where(["blog_id" => $item['blog_id'], 'user_id' => $item['user_id']])->increment("rmb_price", $item['recharge_moeny']);
                }
            }
        });
        return $form;
    }


}
